package com.daluzy.chmanager.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.daluzy.chmanager.R;
import com.daluzy.chmanager.bean.Area;
import com.daluzy.chmanager.bean.School;
import com.daluzy.chmanager.bean.TestPoint;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * 添加管理员
 */
public class AddTestPointActivity extends AppCompatActivity {


    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_test_point_name)
    EditText etTestPointName;
    @BindView(R.id.tv_test_point_area)
    TextView tvTestPointArea;
    @BindView(R.id.tv_add_commit)
    TextView tvAddCommit;
    private String testPointArea;
    private Area area;

    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_test_point_daluzy);
        ButterKnife.bind(this);

        loadingDialog = new LoadingDialog(this);
    }

    @OnClick({R.id.iv_back, R.id.tv_test_point_area, R.id.tv_add_commit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_test_point_area:
                Intent intent = new Intent(AddTestPointActivity.this, SelectAreaActivity.class);
                startActivityForResult(intent, 2021);
                break;
            case R.id.tv_add_commit:
                tvAddCommit.setEnabled(false);
                check();
                break;
        }
    }

    private void check() {
        testPointArea = etTestPointName.getText().toString().trim();
        if (TextUtils.isEmpty(testPointArea)) {
            Toast.makeText(AddTestPointActivity.this, "请输入测试点名称", Toast.LENGTH_SHORT).show();
            tvAddCommit.setEnabled(true);
            return;
        }
        if (area == null) {
            Toast.makeText(AddTestPointActivity.this, "请选择区域", Toast.LENGTH_SHORT).show();
            tvAddCommit.setEnabled(true);
            return;
        }
        addTestPoint();
    }


    private void addTestPoint() {
        TestPoint testPoint = new TestPoint();
        testPoint.setPoint_name(testPointArea);
        testPoint.setPoint_id(String.valueOf(System.currentTimeMillis()));
        testPoint.setArea(area.getArea_id());
        testPoint.setArea_name(area.getArea_name());
        testPoint.save(new SaveListener<String>() {
            @Override
            public void done(String objectId, BmobException e) {
                if (e == null) {
                    Toast.makeText(AddTestPointActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                    finish();
                } else {
                    Toast.makeText(AddTestPointActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
                if (loadingDialog != null)
                    loadingDialog.close();
                tvAddCommit.setEnabled(true);
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2021 && resultCode == 2021) {
            area = (Area) data.getSerializableExtra("area");
            if (area != null)
                tvTestPointArea.setText(area.getArea_name());
        }
    }
}
